<?php
namespace Summer\Sms\Classes;


use Summer\Sms\Models\RecordModel;
use Summer\Sms\Models\Settings;

class CodeApi
{
    public function check($phone,$code,$scene_slug=null){
        $sms_config=Settings::instance();
        $return = [];
        $return['status'] = 1;
        $return['msg'] = '验证成功';
        if($sms_config->is_check!=1){
            $return['status'] = 2;
            $return['msg'] = '验证通过：验证关闭';
            return $return;
        }

        $query=RecordModel::select();
        $query->where('phone',$phone);
        if($scene_slug){
            $query->where('scene_slug',$scene_slug);
        }
        $record=$query->orderBy('id','DESC')->first();
        if(!$record){
            $return['status'] = -1;
            $return['msg'] = '验证失败';
            return $return;
        }
        //失效
        $outime=$sms_config->outime>0?$sms_config->outime:300;//超时时间
        $createtime=$record->created_at->timestamp;
        $now_time=time();
        if(($createtime+$outime)<$now_time){
            $return['status'] = -1;
            $return['msg'] = '验证超时';
            return $return;
        }
        if($code!=$record->code){
            $return['status'] = -1;
            $return['msg'] = '验证码错误';
            return $return;
        }
        return $return;

    }


}
